
function AccordionObj(obj){
	this.editor = obj;
	this.callback = new Array();
}

AccordionObj.prototype.toString = function(){
	return "AccordionObj";
}

AccordionObj.prototype.onReady = function(){
	var $accordion = jQuery(this.editor);
	
	var width = $accordion.attr("width");
	if(width){
		$accordion.css("width",width);
	}
	
	var height = $accordion.attr("height");
	if(height){
		$accordion.css("height",height);
	}
	
	using('accordion', function(){
        $accordion.accordion({});
    });
    

    var that = this;
    $accordion.children("div[JSObjName=ACCORDIONPAGE]").each(function(){
        var $page = jQuery(this);
        var caption = $page.attr("title");
        
        var itemOnClick = $page.attr("itemonclick");
        if(itemOnClick){
            that._setOnClickHandler(caption,itemOnClick);
        }          
        
        
        if($page.attr("data") ){
            var json = $page.attr("data");
            var $table = jQuery("<table>").attr("id","accordion_page_item_table").addClass("accordion_page_item_table");
            var $tbody = jQuery("<tbody>");
            $page.append($table);
            $table.append($tbody);
        
            var dataObj = parseJSON(json);
            var data = dataObj.data;
            for(var i=0; i<data.length; i++){
                var $tr = jQuery("<tr>").attr("url",data[i].url).attr("title",data[i].title).addClass("accordion_page_item_default");
                var $td = jQuery("<td>").text(data[i].title);
                
                //设置tr onclick回调函数
                $tr.bind("click",data[i],function(event){
//                    $tbody.children("tr").removeClass("accordion_page_item_selected");
                	jQuery(".accordion_page_item_selected", $accordion).removeClass("accordion_page_item_selected");
                    jQuery(this).addClass("accordion_page_item_selected");

                    if(that.callback != null){
                        window[that.callback[caption]](jQuery.trim(event.data.url), jQuery.trim(event.data.title));
                    }
                })
                
                $tr.append($td);
                $tbody.append($tr);
            }       
        }
    });
}

AccordionObj.prototype._setOnClickHandler = function(title, func){
	if(title && jQuery.trim(title)!="" &&  func  && jQuery.trim(func)!=""){
		this.callback[title] = func;
	}
}

AccordionObj.prototype.getSelected = function(){
	var id = "#" + this.editor.id;
	return jQuery(id).accordion("getSelected");
}

AccordionObj.prototype.addEventListener = function(event, handler){
    var func;
    if(typeof handler == "function"){
        func = handler;
    }else{
        return;
    }

    var id = "#" + this.editor.id;
    setTimeout(function(){
        if(event == "onSelect"){
            jQuery(id).accordion({onSelect:func});
        }    
    }, 100);

}